<?php
namespace Admin\Controller;

class ShopController extends AdminController
{
    public function index()
    {
        $this->display();
    }

    public function catalogue()
    {
        $category = M('category');
        $res      = $category->where(array('pid' => 0))->select();
        $this->assign('res', $res);
        $this->display();
    }
    public function add()
    {
        if (IS_POST) {
            $sname = D('Sname');
            $cid=I('post.');
            $res   = $sname->create(I('post.'));
            //dump($cid);die;
            if ($res) {
                if ($sname->add()) {
                    $this->success('添加成功', U('Shop/details',array('id' => $cid)));
                } else {
                    $this->error('添加失败', U('Shop/add'));
                }
            } else {
                $this->error($sname->getError());
            }
        } else {
            $cate = D('category')->where(array('pid' => 0))->select();
            $this->assign('cate', $cate);
            $this->display();
        }
    }
    public function getChild()
    {
        $category = M('category');
        $id       = I('get.id');
        $cate     = $category->where(array('pid' => $id))->select();
        if ($cate && $id) {
            $this->assign('cate', $cate);
            $html = $this->fetch('select');

            $return['status'] = 1;
            $return['info']   = 'success';
            $return['data']   = $html;
        } else {
            $return['status'] = 0;
            $return['info']   = 'error';
            $return['data']   = null;
        }
        $this->ajaxReturn($return);

    }
    public function fenlei(){
        $id = I('get.id');
        $category = M('category');
        $res      = $category->where(array('pid' =>$id))->select();
        $this->assign('res', $res);
        $this->display();
    }
    public function details()
    {
        $id = I('get.id');
        //dump($id);    
        $res = D('Sname')->field('shop_sname.* , shop_category.
            pid')->join('shop_category ON shop_category.
            id =shop_sname.cid')->where(array('pid'=>$id))->select();
        //dump($res);
        //dump(D('Sname')->getLastSql());
        $this->assign('res', $res);
        $this->display();
    }
    public function getinput()
    {
        $html           = $this->fetch('addinput');
        $return['data'] = $html;
        $this->ajaxReturn($return);
    }
    public function attribute()
    {
        $id = I('get.id');
        if (IS_POST) {
            $kname  = D('Kname');
            $id     = I('post.gid');
            $pkname = I('post.kname');
            $count  = count($pkname);
            $res    = $kname->create(I('post.'));
            if ($res) {
                if ($pkname) {
                    for ($i = 0; $i < $count; $i++) {
                        echo $kname->add(array('kname' => $pkname[($i)], 'gid' => $id));
                    }
                    $this->success('添加成功', U('val',array('id' => $id[0])));
                } else {
                    $this->error('添加失败', U('attribute'));
                }
            } else {
                $this->error($kname->getError());
            }
        } else {
            $this->assign('id', $id);
            $this->display();
        }
    }
    public function val()
    {
        $id = I('get.id');
        if (IS_POST) {
            $val    = D('Val');
            $kid    = I('post.kid');
            $gid    = I('post.gid');
            $pvname = I('post.vname');
            $count  = count($pvname);
            $res    = $val->create(I('post.'));
            if ($res) {
                if ($pvname) {
                    for ($i = 0; $i < $count; $i++) {
                        echo $val->add(array('vname' => $pvname[($i)], 'kid' => $kid[($i)], 'gid' => $gid[($i)]));
                    }
                    $this->success('添加成功', U('much', array('id' => $gid[0])));
                } else {
                    $this->error('添加失败', U());
                }
            } else {
                $this->error($val->getError());
            }
        } else {
            //dump($id);
            $res = D('Kname')->where(array('gid' => $id))->select();
            //dump($res);
            $this->assign('id', $id);
            $this->assign('res', $res);
            $this->display();
        }

    }
    public function getinval()
    {
        $this->assign('kid', I('get.kid'));
        $this->assign('gid', I('get.gid'));
        $html           = $this->fetch('valinput');
        $return['data'] = $html;
        $this->ajaxReturn($return);
    }
    public function much()
    {
        if (IS_POST) {
            $details = D('Details');
            $gid     = I('post.gid');
            $name    = I('post.name');
            $price   = I('post.price');
            $vid     = I('post.vid');
            $count   = I('post.count');
            $time    = I('post.ctime');
            $dcount  = count($name);
            $res     = $details->create(I('post.'));
            if ($res) {
                if ($name) {
                    for ($i = 0; $i < $dcount; $i++) {
                        echo $details->add(array('name' => $name[($i)], 'gid' => $gid[($i)], 'vid' => $vid[($i)], 'price' => $price[($i)], 'count' => $count[($i)], 'ctime' => $time[($i)]));
                    }
                    $this->success('添加成功', U('smuch', array('id' => $gid[0])));
                } else {
                    $this->error('添加失败', U());
                }
            } else {
                $this->error($details->getError());
            }
        } else {
            $gid     = I('get.id');
            $res     = D('val')->where(array('gid' => $gid))->select();
            $vresult = array();
            $kresult = array();
            $ckresult = array();
            foreach ($res as $v) {
                $vresult[$v['kid']][] = $v['vname'];
            }
            foreach ($res as $v) {
                $kresult[$v['kid']][] = $v['id'];
            }
            foreach ($res as $v) {
                $ckresult[]=$v['kid'];
            }
            //dump($ckresult);
            $ck =array_unique($ckresult);
            $count=count($ck);
            $time     = time();
            if($count>1){
            $varr     = array_values($vresult);
            $karr     = array_values($kresult);
            //dump($varr);
            $vdetails = $this->CartesianProduct($varr);
            $kdetails = $this->CartesianProduct($karr);
            //dump($vdetails);
            $this->assign('vdetails', $vdetails);
            $this->assign('kdetails', $kdetails);
          }else{
            $vdetails=$vresult[$ck[0]];
            $kdetails=$kresult[$ck[0]];
            //dump($kresult);
            $this->assign('vdetails', $vdetails);
            $this->assign('kdetails', $kdetails);
          }
            $this->assign('time', $time);
            $this->assign('gid', $gid);
            $this->display();
        }
    }

    public function CartesianProduct($sets)
    {
        // 保存结果
        $result = array();
        // 循环遍历集合数据
        for ($i = 0, $count = count($sets); $i < $count - 1; $i++
        ) {
            // 初始化
            if ($i == 0) {
                $result = $sets[$i];
            }
            // 保存临时数据
            $tmp = array();
            // 结果与下一个集合计算笛卡尔积
            foreach ($result as $res) {
                foreach ($sets[$i + 1] as $set) {
                    $tmp[] = $res . ',' . $set;
                }
            }
            // 将笛卡尔积写入结果
            $result = $tmp;
        }
        return $result;
    }
    public function smuch()
    {
        $gid     = I('get.id');
        $details = D('Details');
        $res     = $details->field('shop_details.*,shop_sname.title')->join('shop_sname ON shop_details.gid =shop_sname.id')->/*join('shop_img ON shop_details.id =shop_img.did')->*/where(array('shop_details.gid'=>$gid))->select();
        //dump($res);
        //dump($details->getLastSql());
        $this->assign('res', $res);
        $this->assign('gid', $gid);
        $this->display();
    }
    public function upmuch()
    {
        $details = D('Details');
        if (IS_POST) {
            $id  = I('post.id');
            $gid = I('post.gid');
            $res = $details->create(I('post.'));
            //dump($_POST['file'][0]);die;
            if ($res) {
                if ($details->where(array('id' => $id))->save()) {
                    //$s=$details->getLastSql();
                    //dump($s);die;
                    $this->success('编辑成功', U('smuch', array('id' => $gid)));
                } else {
                    $this->error('编辑失败', U());
                }
            } else {
                $this->error($details->getError());
            }
        } else {
            $id  = I('get.id');
            $res = $details->where(array('id' => $id))->select();
            //$s=$details->getLastSql();
            $time = time();
            $this->assign('time', $time);
            $this->assign('res', $res);
            $this->display();
        }
    }
    public function upload()
    {
        $upload           = new \Think\Upload(); // 实例化上传类
        $upload->maxSize  = 3145728; // 设置附件上传大小
        $upload->exts     = array('jpg', 'gif', 'png', 'jpeg'); // 设置附件上传类型
        $upload->rootPath = './Uploads/'; // 设置附件上传根目录
        // 上传文件
        $info = $upload->upload();
        if (!$info) {
            // 上传错误提示错误信息
            $this->error($upload->getError());
        } else {
            // 上传成功
            //$this->success('上传成功！');
            return $info;
        }
    }
    public function img(){
        $did=I('get.id');
        $gid=I('get.gid');
        //dump($did);
        //dump($gid);
        $res=D('Img')->field('shop_img.*,shop_sname.title,shop_details.name')->join('shop_sname ON shop_img.gid =shop_sname.id')->join('shop_details ON shop_img.did =shop_details.id')->where(array('shop_img.gid'=>$gid,'shop_img.did'=>$did))->select();
        //dump(D('Img')->getLastSql());
        $this->assign('did',$did);
        $this->assign('gid',$gid);
        $this->assign('res',$res);
        $this->display();
    }
    public function addimg(){
        $did=I('get.did');
        $gid=I('get.gid');
        $details=D('Details');
        $res     = $details->field('shop_details.*,shop_sname.title')->join('shop_sname ON shop_details.gid =shop_sname.id  ')->where(array('gid'=>$gid,'shop_details.id'=>$did))->select()[0];
        if(IS_POST){
          $img=D('Img');
          $gid = I('post.gid');
          $did = I('post.did');
          //dump($did);die;
          $pic = $this->upload();
          //dump($pic);
          $count=count($pic);
          if($pic){
               if($count){
                for($i=0;$i<$count;$i++){
                  $_POST['file'] = $pic[$i]['savepath'] . $pic[$i]['savename'];
                  //dump($_POST['file']);die;
                  $rimg = $img->create(I('post.'));
                  //dump($_POST['file']);
                  $img->where(array('gid' => $gid,'did' => $did,'file' => $_POST['file']))->add();
                  //dump($img->getLastSql());die;
             }
                $this->success('添加图片成功',U('img',array('id'=>$did,'gid'=>$gid)));
             }else{
                $this->error('添加图片失败');
             }
               
          }else{
             $this->error($img->getError());
          }

        }else{
          $this->assign('res',$res);
          $this->display();  
        }
        
    }
    public function addpic(){
        $gid=I('get.gid');
        $details=D('Details');
        $res     = $details->field('shop_details.*,shop_sname.title')->join('shop_sname ON shop_details.gid =shop_sname.id  ')->where(array('gid'=>$gid))->select()[0];
        if(IS_POST){
          $img=D('Details');
          $gid = I('post.gid');
          //dump($did);die;
          $pic = $this->upload();
          $_POST['pic'] = $pic[0]['savepath'] . $pic[0]['savename'];
          $rimg = $img->field('pic')->create(I('post.'));
          //dump($rimg);
          if($rimg){
                $dimg=$img->where(array('gid' => $gid))->save();
                //dump($dimg);die;
               if($dimg){
                  //dump($img->getLastSql());die;
             
                $this->success('添加图片成功',U('smuch',array('id'=>$gid)));
             }else{
                $this->error('添加图片失败');
             }
               
          }else{
             $this->error($img->getError());
          }

        }else{
          $this->assign('res',$res);
          $this->display();  
        }
        
    }
}
